package com.zoweunion.mechanic.controller.base;

import com.alibaba.fastjson.JSONObject;
import com.zoweunion.mechanic.controller.BaseController;
import com.zoweunion.mechanic.model.ResponseBean;
import com.zoweunion.mechanic.model.User;
import com.zoweunion.mechanic.service.base.BlackEnclosureService;
import com.zoweunion.mechanic.util.MyException;
import com.zoweunion.mechanic.util.constants.Constants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @Description: 黑区电子围栏-金融平台
 * @author: nh
 * @date: 2022/10/13 10:32
 */
@RestController
@RequestMapping("/blackEnclosure")
public class BlackEnclosureController extends BaseController {

    @Autowired
    private BlackEnclosureService blackEnclosureService;

    /**
     * 新增黑区电子围栏
     */
    @PostMapping("/addBlackEnclosure")
    public ResponseBean addBlackEnclosure(@RequestBody String str, HttpServletRequest request) throws MyException {
        logBefore(logger, "黑区电子围栏_添加黑区电子围栏");
        Map<String, Object> reqMap = JSONObject.parseObject(str, Map.class);
        User user = getCurrentUser(request);
        reqMap.put("s_id",user.getS_id());
        reqMap.put("u_id",user.getId());
        reqMap.put("area",user.getArea());
        int result = blackEnclosureService.addBlackEnclosure(reqMap);
        logAfter(logger);
        return new ResponseBean(Constants.CODE_SUCCESS,"操作成功",result);
    }

    /**
     *  获取黑区电子围栏列表
     *   radius: 围栏半径 lnglat：围栏经纬度
     */
    @PostMapping("/getBlackEnclosureList")
    public ResponseBean getBlackEnclosureList(@RequestBody String str, HttpServletRequest request) throws MyException {
        logBefore(logger, "黑区电子围栏_获取黑区电子围栏列表");
        Map<String, Object> reqMap = JSONObject.parseObject(str, Map.class);
        User user = getCurrentUser(request);
        reqMap.put("s_id", user.getS_id());
        if(reqMap.get("server_type") == null || !(Integer.valueOf(reqMap.get("server_type").toString()) == 4)) {
            reqMap.put("area", user.getArea());
        }
        Map<String, Object> resultMap = blackEnclosureService.getBlackEnclosureList(reqMap);
        if(resultMap==null){
            return new ResponseBean(Constants.CODE_SUCCESS,"无数据",null);
        }
        logAfter(logger);
        return new ResponseBean(Constants.CODE_SUCCESS,"操作成功",resultMap);
    }

    /**
     * 获取黑区电子围栏的详情
     * @param str
     * @return
     * @throws MyException
     */
    @PostMapping("/getBlackEnclosureInfo")
    public ResponseBean getBlackEnclosureInfo(@RequestBody String str) throws MyException {
        logBefore(logger, "黑区电子围栏_获取黑区电子围栏的详情");
        Map<String, Object> reqMap = JSONObject.parseObject(str, Map.class);
        Map<String, Object> resultMap = blackEnclosureService.getBlackEnclosureInfo(reqMap);
        logAfter(logger);
        return new ResponseBean(Constants.CODE_SUCCESS,"操作成功",resultMap);
    }

    /**
     *  删除黑区电子围栏
     *
     */
    @PostMapping("/delBlackEnclosureList")
    public ResponseBean delBlackEnclosureList(@RequestBody String str) throws MyException {
        logBefore(logger, "黑区电子围栏_删除黑区电子围栏");
        Map<String, Object> reqMap = JSONObject.parseObject(str, Map.class);
        int result = blackEnclosureService.delBlackEnclosureList(reqMap);
        logAfter(logger);
        return new ResponseBean(Constants.CODE_SUCCESS,"操作成功",result);
    }
}
